package com.wiegand {
	import com.wiegand.tracking.GoogleAnalyticsTracker;
	import com.asual.swfaddress.SWFAddress;
	import com.caurina.transitions.Tweener;
	import com.wiegand.content.ContentManager;
	import com.wiegand.content.background.Background;
	import com.wiegand.content.overlay.Footer;
	import com.wiegand.content.overlay.Shadows;
	import com.wiegand.event.LoaderEvent;
	import com.wiegand.gui.Logo;
	import com.wiegand.gui.navigation.MainNavigation;
	import com.wiegand.loader.ImageLoader;
	import com.wiegand.util.ColorRectangle;
	import com.wiegand.util.SharedObjectManager;

	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.display.StageDisplayState;
	import flash.events.MouseEvent;
	import flash.geom.Point;

	/**
	 * @author gandy
	 */
	public class Main extends AbstractMain {

		private var _backgoundContainer : Sprite;
		private var _contentContainer : Sprite;
		private var _overlayContainer : Sprite;
		
		private var _background : Background;
		private var _logo : Logo;
		private var _shadows : Shadows;
		private var _footer : Footer;
		private var _navigation : MainNavigation;
		private var _contentManager : ContentManager;
		private var _overlayBlend : Sprite;

		public function Main() {
			super();
		}
		
		override protected function startApplication() : void{
			SharedObjectManager.getInstance().initinalize("wie-gand-interactive");
			
			GoogleAnalyticsTracker.getInstance().activate(String(_initXml.googleAnalyticsID), this);
			
			Configuration.getInstance().elements = _initXml.elements;
			Configuration.getInstance().main = this;
			Configuration.getInstance().stage = stage;
			
			_objectsToResize = new Array();
			
			_backgoundContainer = new Sprite();
			_backgoundContainer.mouseEnabled = false;
			_contentLayer.addChild( _backgoundContainer );
						
			_contentContainer = new Sprite();
			_contentContainer.mouseEnabled = true;
			_contentLayer.addChild( _contentContainer );
			
			_overlayContainer = new Sprite();
			_overlayContainer.mouseEnabled = false;
			_contentLayer.addChild( _overlayContainer );
			
			ImageLoader.getInstance().addEventListener(LoaderEvent.EVENT_LOADING_COMPLETE, imagesLoaded);
			ImageLoader.getInstance().load([_initXml.elements.images.image], [_initXml.elements.images.image.@name]);			
		}
				
		private function imagesLoaded(e:LoaderEvent) : void {
			ImageLoader.getInstance().removeEventListener(LoaderEvent.EVENT_LOADING_COMPLETE, imagesLoaded);
			buildBackground();
			buildContent();
			buildOverlay();
		}

		private function buildBackground() : void {
			_background = new Background();
			_backgoundContainer.addChild( _background );
			Configuration.getInstance().dict.add(_background, "Background");
			_background.init();
			_background.alpha = 0;
			Tweener.addTween(_background,{alpha:1,time:2,transition:"linear"});
			Tweener.addTween(_background,{alpha:1,time:5,transition:"linear",onComplete:drawLine});
			
		}
		
		private function drawLine():void{
			_background.updateLines(Configuration.getInstance().elements.lines);
		}
			
		private function buildContent() : void {
			_contentManager = new ContentManager();
			_contentContainer.addChild(_contentManager);
		}
		
		private function buildOverlay() : void {
			_shadows = new Shadows();
			_shadows.mouseEnabled = false;
			_overlayContainer.addChild( _shadows );
			Configuration.getInstance().dict.add(_shadows, "Shadows");
			_shadows.init();
			_shadows.alpha = 0;
			Tweener.addTween(_shadows,{alpha:1,delay:1,time:1,transition:"linear"});
			
			_footer = new Footer();
			_footer.mouseEnabled = false;
			_overlayContainer.addChild( _footer );
			_footer.init();
			_footer.y = 100;
			Configuration.getInstance().dict.add(_footer, "Footer");
			Tweener.addTween(_footer,{y:0,delay:3,time:2,transition:"easeoutback"});
			
			_navigation = new MainNavigation(Configuration.getInstance().elements.mainNavigation, blendContent);
			_navigation.mouseEnabled = false;
			_navigation.x=200;
			_navigation.y=200;
			addChild(_navigation);
			_navigation.init();
			_navigation.open(5);
			
			_overlayBlend = ColorRectangle.create(0x000000,1,1,1);
			_overlayBlend.mouseEnabled = false;
			_overlayBlend.alpha = 0;
			_overlayContainer.addChild( _overlayBlend );
			
			var bmp : Bitmap = ImageLoader.getInstance().getBitmapByName( _initXml.elements.images.image.@name );
			_logo = new Logo(new Point(Configuration.BORDER,-200), new Point(Configuration.BORDER,-85), bmp);
			_logo.open(3);
			_overlayContainer.addChild(_logo);
			_logo.buttonMode = true;
			_logo.addEventListener(MouseEvent.CLICK, logoClick);
			_logo.addEventListener(MouseEvent.MOUSE_OVER, logoOver);
			_logo.addEventListener(MouseEvent.MOUSE_OUT, logoOut);
		}
		
		private function logoClick(e:MouseEvent) : void {
			SWFAddress.setValue(_initXml.elements.contents.@startContent);
			blendContent(false);
		}
		
		private function logoOver(e:MouseEvent) : void {
			Tweener.removeTweens(_logo);
			Tweener.addTween(_logo,{y:15,time:.5,transition:"easeoutback"});
			blendContent(true);
		}
		
		private function logoOut(e:MouseEvent) : void {
			Tweener.removeTweens(_logo);
			Tweener.addTween(_logo,{y:0,time:.5,transition:"easeinback"});
			blendContent(false);
		}

		public function blendContent(b:Boolean):void{
			_overlayBlend.width = Configuration.getInstance().stage.stageWidth;
			_overlayBlend.height = Configuration.getInstance().stage.stageHeight;
			if(b){
				_overlayBlend.mouseEnabled = true;
				Tweener.removeTweens(_overlayBlend);
				Tweener.addTween(_overlayBlend,{alpha:.5,time:.5,transition:"linear"});			
			}else{		
				_overlayBlend.mouseEnabled = false;	
				Tweener.removeTweens(_overlayBlend);
				Tweener.addTween(_overlayBlend,{alpha:.0,time:.5,transition:"linear"});			
			}
		}

//		public function changeContent(name:String) : void {
//			trace("changeContent: " + name);
//			_contentManager.changeTo(name);
//		}	

		public function changeDisplayMode(fullscreen:String=null):void{
			switch(fullscreen){
				case "true"		:		stage.displayState = StageDisplayState.FULL_SCREEN; break;
				case "false"	:		stage.displayState = StageDisplayState.NORMAL; break;
				default			:		break;
			}
		}
	}
}
